﻿using System.Collections;
using UnityEngine;
using XLua;

namespace XLua_Ki
{
    /// 加载音乐/音效
    public class AudioManager_Ki : MonoBehaviour
    {
        private float musicValue;   //BGM音量
        public float MusicValue
        {
            get { return musicValue; }
            set { musicValue = value; }
        }
        private float voiceValue;   //音效音量
        public float VoiceValue
        {
            get { return voiceValue; }
            set { voiceValue = value; }
        }

        /// 初始化音量默认大小.[获取存档数据]
        public void StartInitialize(float musicValue, float voiceValue)
        {
            this.musicValue = musicValue;
            this.voiceValue = voiceValue;
        }

        /// 生成背景音乐(无特效)
        public void CreateAudioMusic(string name, Vector3 startPos, Vector3 startRotation, Transform parent = null, LuaFunction luaFunction = null)
        {
            StartCoroutine(OnCreateMusic(name, startPos, startRotation, parent, luaFunction));
        }

        IEnumerator OnCreateMusic(string name, Vector3 startPos, Vector3 startRotation, Transform parent, LuaFunction luaFunction)
        {
            var assetName = name.ToLower();
            GameObject obj = GameManager_Ki.Instance.GetPoolManager_Ki().ReturnScence(GameManager_Ki.Instance.GetResourcesManager_Ki().Prefabs_AB_GAME[assetName], parent);
            if (parent == null)
            {
                obj.transform.localPosition = startPos;
                obj.transform.localEulerAngles = startRotation;
            }

            var m_AudioBehaviour = obj.GetComponent<AudioBehaviour>();
            if (m_AudioBehaviour == null)
            {
                m_AudioBehaviour = obj.AddComponent<AudioBehaviour>();
            }

            m_AudioBehaviour.StartPlayVoice(musicValue, true); //播放背景音乐

            yield return null;
            if (luaFunction != null) luaFunction.Call(obj);

        }

        /*
        /// 生成(音效)特效
        public void CreateEffect(string name, Vector3 startPos, Vector3 startRotation, Transform parent = null, LuaFunction luaFunction = null)
        {
            StartCoroutine(OnCreateEffect(name, startPos, startRotation, parent, luaFunction));
        }

        IEnumerator OnCreateEffect(string name, Vector3 startPos, Vector3 startRotation, Transform parent, LuaFunction luaFunction)
        {
            var assetName = name.ToLower();   
            GameObject obj = null;
            if (GameManager.Instance.GetResourcesManager().Prefabs_Game.ContainsKey(assetName))
            {
                obj = GameManager.Instance.GetPoolManager().ReturnScence(GameManager.Instance.GetResourcesManager().Prefabs_Game[assetName], parent);
            }
            else
            {
                var effectName = GameManager.Instance.GetResourcesManager().Prefabs_Model_Info[name].ModelEffectName;
                obj = GameManager.Instance.GetPoolManager().ReturnScence(GameManager.Instance.GetResourcesManager().Prefabs_Game[effectName.ToLower()], parent);
            }

            if (parent == null)
            {
                obj.transform.localPosition = startPos;
                obj.transform.localEulerAngles = startRotation;
            }

            var m_AudioBehaviour = obj.GetComponent<AudioBehaviour>();
            if (m_AudioBehaviour == null)
            {
                m_AudioBehaviour = obj.AddComponent<AudioBehaviour>();
            }

            m_AudioBehaviour.StartPlayEffect(voiceValue);  //播放特效
            yield return null;
            if (luaFunction != null) luaFunction.Call(obj);  
        }

        */
    }
}
